// Fetch tip & save in storage
const updateTip = async () => {
    try {
        const response = await fetch('http://localhost:9005/rules.json');
        const rules = await response.json();
        return chrome.storage.local.set({ rules });
    } catch (error) {}
};

const ALARM_NAME = 'getRules';

// Check if alarm exists to avoid resetting the timer.
// The alarm might be removed when the browser session restarts.
async function createAlarm() {
    const alarm = await chrome.alarms.get(ALARM_NAME);
    if (typeof alarm === 'undefined') {
        chrome.alarms.create(ALARM_NAME, {
            delayInMinutes: 5 / 60, // 延迟 5 秒 (5 秒 = 5/60 分钟)
            periodInMinutes: 1 / 6, // 每 10 秒触发一次 (10 秒 = 1/6 分钟)
        });
        updateTip();
    }
}

createAlarm();

// Update tip once a day
chrome.alarms.onAlarm.addListener(updateTip);
